<?php

// by masphei

class Login_con extends CI_Controller{
    //put your code here
    function __construct() {
        parent::__construct();
    }
    
    function index(){
        $this->login();
    }
    
    function login(){
        $data_login = array(
                'nama'=>"",
                'foto'=>"",
                'islogin'=>FALSE
            );
        $this->session->set_userdata($data_login);
        $this->load->view('progress/login_view');
    }
    
    function admin_login(){
        $data_login = array(
                'nama'=>"",
                'foto'=>"",
                'islogin'=>FALSE
            );
        $this->session->set_userdata($data_login);
        $this->load->view('progress/admin/login_view');
    }
    
    function login_process(){
        $usr = $this->input->post('usr');
        $pwd = md5($this->input->post('pwd'));
        
//        $str_query = 'select * from user natural join role where USERNAME="'.$usr.'" and PASSWORD="'.$pwd.'" and ID_ROLE=6';
//        $result = $this->db->query($str_query);
//        if($result->num_rows()==1){
//            $data_user = $result->row_array();
//            $data_login = array(
//                'id'=>$data_user['ID'],
//                'nama'=>$data_user['NAMA'],
//                'foto'=>$data_user['FOTO'],
//                'islogin'=>TRUE
//            );
//            $this->session->set_userdata($data_login);
//            redirect(base_url().'progress/admin_con/');
//        }else{
            $query = $this->db->query('select b.ID as ID, b.NAMA as NAMA, b.FOTO as FOTO from progress_user as a inner join user as b on b.ID=a.ID_USER where b.USERNAME="'.$usr.'" and b.PASSWORD="'.$pwd.'"')->result_array();
            if(count($query)==1){
                $data_user = $query;
                $data_login = array(
                    'id'=>$data_user[0]['ID'],
                    'nama'=>$data_user[0]['NAMA'],
                    'foto'=>$data_user[0]['FOTO'],
                    'islogin'=>TRUE
                );
                $this->session->set_userdata($data_login);
                
            }
            redirect(base_url().'progress/progress_con/');
//            else
//                redirect(base_url().'progress/admin');
//        }
    }
    
    function admin_login_process(){
        $usr = $this->input->post('usr');
        $pwd = md5($this->input->post('pwd'));
        
        $str_query = 'select * from user natural join role where USERNAME="'.$usr.'" and PASSWORD="'.$pwd.'" and ID_ROLE=6';
        $result = $this->db->query($str_query);
        if($result->num_rows()==1){
            $data_user = $result->row_array();
            $data_login = array(
                'id'=>$data_user['ID'],
                'nama'=>$data_user['NAMA'],
                'foto'=>$data_user['FOTO'],
                'islogin'=>TRUE
            );
            $this->session->set_userdata($data_login);
            redirect(base_url().'progress/admin_con/');
        }else{
            redirect(base_url().'progress/admin');
        }
    }
    
    function logout(){
        $data_login = array(
                'nama'=>"",
                'foto'=>"",
                'islogin'=>FALSE
            );
        $this->session->set_userdata($data_login);
        redirect(base_url().'progress');
    }

}